# Maintainer: Alexey Pavlov <alexpux@gmail.com>

PKGEXT='.pkg.tar.xz'
pkgname=mintty
pkgver=3.2.0
pkgrel=1
epoch=1
pkgdesc="Terminal emulator with native Windows look and feel"
arch=('i686' 'x86_64')
license=('GPL3')
depends=('sh')
url="https://mintty.github.io"
source=(${pkgname}-${pkgver}.tar.gz::https://github.com/mintty/mintty/archive/${pkgver}.tar.gz
        0002-add-msys2-launcher.patch)
sha256sums=('5423320eea63aff00e3a10924848323466075381ac1e7ea925356af3fe771528'
            'dbf0eecfcd7a02361ed1b86409b77b759473ae37f29a91dab5028b149d7dac1c')

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}

prepare() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  del_file_exists src/launcher.c src/launcher.h
  patch -p1 -i ${srcdir}/0002-add-msys2-launcher.patch
#  patch -p1 -i ${srcdir}/8be3ff0d918058b56ba0d1bc26eea25b875ab929.patch
}

build() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  make
}

package() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  mkdir -p ${pkgdir}/usr/{bin,share}
  mkdir -p ${pkgdir}/usr/share/man/man1
  mkdir -p ${pkgdir}/usr/share/licenses/${pkgname}
  mkdir -p ${pkgdir}/usr/share/${pkgname}/lang
  mkdir -p ${pkgdir}/usr/share/${pkgname}/themes
  mkdir -p ${pkgdir}/usr/share/${pkgname}/icon
  mkdir -p ${pkgdir}/usr/share/${pkgname}/sounds
  mkdir -p ${pkgdir}/usr/share/mintty/emojis

  install -m755 bin/mintty.exe ${pkgdir}/usr/bin/mintty.exe
  install -m755 tools/mintheme ${pkgdir}/usr/bin/
  install -m644 docs/mintty.1 ${pkgdir}/usr/share/man/man1/mintty.1

  install -m644 LICENSE ${pkgdir}/usr/share/licenses/${pkgname}/
  install -m644 LICENSE.Oxygen ${pkgdir}/usr/share/licenses/${pkgname}/
  install -m644 LICENSE.PuTTY ${pkgdir}/usr/share/licenses/${pkgname}/

  install -m644 lang/messages.pot ${pkgdir}/usr/share/${pkgname}/lang/
  install -m644 lang/*.po ${pkgdir}/usr/share/${pkgname}/lang/

  install -m644 themes/* ${pkgdir}/usr/share/${pkgname}/themes/

  install -m644 tools/getemojis ${pkgdir}/usr/share/mintty/emojis

  install -m644 icon/wsl.ico ${pkgdir}//usr/share/${pkgname}/icon
  for i in 16 24 32 48 64 256
  do
    mkdir -p ${pkgdir}/usr/share/icons/hicolor/${i}x${i}/apps
    install -m644 icon/hi${i}-apps-mintty.png \
                 ${pkgdir}/usr/share/icons/hicolor/${i}x${i}/apps/mintty.png
  done
  install -m644 sounds/*.wav ${pkgdir}/usr/share/${pkgname}/sounds/
  install -m644 sounds/*.WAV ${pkgdir}/usr/share/${pkgname}/sounds/
  install -m644 sounds/*.md ${pkgdir}/usr/share/${pkgname}/sounds/
}
